#include "Aac_Decoder.h"

AAC_Decoder::AAC_Decoder(AVContext *avc,QObject *parent)
    :QThread(parent)
    ,avContext_(avc)
{
    //创建重采样对象
    audioResampler_.reset(new AudioResampler);
}

AAC_Decoder::~AAC_Decoder()
{
    Close();
}

int AAC_Decoder::Open(const AVCodecParameters *codecParmer)
{
    //初始化解码器
    if(codec_ || codecContext_)
    {
        return -1;
    }
    //创建解码器
    codec_ = const_cast<AVCodec*>(avcodec_find_decoder(codecParmer->codec_id));
    if(!codec_)
    {
        return -1;
    }
    //创建解码器上下文
    codecContext_ = avcodec_alloc_context3(codec_);
    //复制解码器上下文
    if(avcodec_parameters_to_context(codecContext_,codecParmer)<0)
    {
        return -1;
    }
    //设置解码加速
    codecContext_->flags |= AV_CODEC_FLAG2_FAST;
    //打开解码器
    if(avcodec_open2(codecContext_,codec_,nullptr)!= 0)
    {
        return -1;
    }

    //设置音频参数
    avContext_->audio_channels = codecContext_->channels;
    avContext_->audio_fmt = AV_SAMPLE_FMT_S16;
    avContext_->audio_sample_rate = 44100;
    //初始化重采样对象
    if(!audioResampler_->Open(codecContext_->sample_rate,codecContext_->channels,codecContext_->sample_fmt,
                                44100,2,AV_SAMPLE_FMT_S16))
    {
        return -1;
    }
    start();//
    return 0;
}



void AAC_Decoder::Close()
{
    //停掉线程，释放资源
    quit_ = true;
    if(isRunning())
    {
        this->quit();
            this->wait();//等待资源释放
    }
}

void AAC_Decoder::run()
{

}

